package com.atguigu.flink.chapter04_window.agg;

import com.atguigu.flink.pojo.MyAcc;
import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;
import org.apache.flink.util.Collector;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
 * Created by Smexy on 2022/10/25
 *
        TopN: 没法滚动聚合。

        求每种传感器每5个中vc最高的三个
            top3

        全量，只针对某些特殊的场景，例如topN.
        用的最多的还是增量，增量最高效。
 *
 */
public class Demo14_Process
{
    public static void main(String[] args) {


        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env
           .socketTextStream("hadoop103", 8888)
           .map(new MapFunction<String, WaterSensor>()
           {
               @Override
               public WaterSensor map(String value) throws Exception {
                   String[] data = value.split(",");
                   return new WaterSensor(
                       data[0],
                       Long.valueOf(data[1]),
                       Integer.valueOf(data[2])
                   );
               }
           })
           .keyBy(WaterSensor::getId)
           .countWindow(5)
           .process(new ProcessWindowFunction<WaterSensor, String, String, GlobalWindow>()
           {
               // Iterable<WaterSensor> elements: 汇总了窗口中的所有的元素
               @Override
               public void process(String key, Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {

                   System.out.println("Demo14_Process.process");
                   List<WaterSensor> top3 = StreamSupport.stream(elements.spliterator(), true) //把Iterable转为Stream
                                                            .sorted(new Comparator<WaterSensor>()  //排序
                                                            {
                                                                @Override
                                                                public int compare(WaterSensor o1, WaterSensor o2) {
                                                                    //默认调用的是升序
                                                                    return -o1.getVc().compareTo(o2.getVc());
                                                                }
                                                            })
                                                            .limit(3)  //取前三
                                                            .collect(Collectors.toList());

                   out.collect(key + ":" +top3.toString());


               }
           })

           .print();

        try {
                    env.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }

    }


}
